﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WorkFlowSystem.DataBase;
using System.Drawing;
using System.Data.SqlClient;

namespace WorkFlowSystem.Business
{
    class WorkInfoDv
    {
        //一览画面DATA取得
        public int getWorkData(bool isInitFlag , string uid, string year , string month , string status , ref DataTable dt,ref int dataCnt)
        {
            if (isInitFlag)
            {   
                //初始化时显示15条空记录
                for (int i = 0; i < 15; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                }                
            }
            else 
            {
                if (month.Length == 1)
                {
                    month = "0" + month;
                }
                
                int yearMonth = Int32.Parse(year + month);

                DBOperator dbo = new DBOperator();
                
                dbo.ConnectDB();
                //update_uid 取更新者姓名
                string sql = " SELECT s.s_name, w.w_start_date, w.w_end_date , " +
                             " case when w.w_date_name = 'D' then " +
                             " CONVERT(varchar(10), w.w_start_date, 120)" +
                             " else w.w_date_name end w_date_name , " +
                             "w.w_status , u.u_name , w.w_update_time, w.w_uid, s.sid " +
                             " FROM  work_info w , user_info u,standard s " +
                             " WHERE w.w_sid = s.sid and " +
                             " s.s_tab_no = 1 and " +
                             " w.w_uid = @uid and " +
                             " w.w_update_uid = u.uid and " +   //取更新者用户名
                             " @yearMonth between year(w_start_date)*100+month(w_start_date) and  year(w_end_date)*100+month(w_end_date)";

                List<SqlParameter> para_list = new List<SqlParameter>();
                
                if(!"所有状态".Equals(status))
                {
                   sql += " and w.w_status = @status ";

                   para_list.Add(new SqlParameter("@status", status));
                }
                
                para_list.Add(new SqlParameter("@uid", uid));
                para_list.Add(new SqlParameter("@yearMonth", yearMonth));


                dbo.ExecuteDataTable(sql, para_list, false, ref dt);

                dbo.CloseDB();

                dataCnt = dt.Rows.Count;

                int dtRowCount = dt.Rows.Count;


                //查询数据如果不满15条，补充数据到15条。
                if (dtRowCount < 15)
                {
                    for (int i = 0; i < 15 - dtRowCount; i++)
                    {
                        dt.Rows.Add(dt.NewRow());
                    }
                }
                
            }
    
            return 0;

        }
        
    }
}
